<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-9-23
 * Time: 下午2:13
 */

class BuyModel extends BaseModel
{
    private $_table;
    protected $_name = 'iq_inquiry';
    protected $_isbug = false;


    public function getInquireCountByWhere($wheres,$group,$having) {
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        if(!$wheres){
            $wheres=" 1=1 ";
        }

        $sql = $db->select ()->from ( $this->_name ,array("product_id","count(*) as num"))
            ->joinLeft ( "product", "$this->_name.product_id = product.id","")
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "" )
            ->joinLeft ( "users_invoice", "$this->_name.business_id = users_invoice.id","")
            ->joinLeft("res_currency","res_currency.id = $this->_name.currency_id","")
            ->joinLeft("res_service","res_service.id = $this->_name.currency_id","")
            ->joinLeft("region","region.region_id = $this->_name.area_id","")
            ->where ( $wheres )
            ->__toString ();

        if($group){
            $sql.= " group  by ".$group;
        }

        if($having){
            $sql.=" HAVING {$having}";
        }

        $sql = "select count(*) from(".$sql.")  as total";

        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }

        return $db->fetchOne ( $sql );
    }


    /**
     * 获取列表
     * @param int $start
     * @param int $count
     * @param string $where
     * @param string $order
     * @return array
     */
    public function getInquirePageListByWhere($where,$item_page,$offset,$order,$group,$having){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ($this->_name,array("id","product_id","business_id","currency_id","service_id","area_id","update_time","num","price","delivery_area","delivery_time","status"))
            ->joinLeft ( "product", "$this->_name.product_id = product.id", array("product_name"))
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "cate_name" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "brand_name" )
            ->joinLeft ( "users_invoice", "$this->_name.business_id = users_invoice.id",array ( "business_name","contacts","mobile","qq"))
            ->joinLeft("res_currency","res_currency.id = $this->_name.currency_id","name as currency_name")
            ->joinLeft("res_service","res_service.id = $this->_name.currency_id","service_name")
            ->joinLeft("region","region.region_id = $this->_name.area_id","region_name as area_name")
            ->where ($where)
            ->__toString ();

        if($group){
            $sql.= " group  by ".$group;
        }

        if($having){
            $sql.=" HAVING {$having}";
        }
        if($order){
            $sql.= " order  by ".$order;
        }
        if($item_page){
            $sql.= " limit $offset,$item_page";
        }

        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchAll ( $sql );

    }
    //会员页面求购显示
    public function getuserList(){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name)->joinLeft ( "product", "$this->_name.product_id = product.id", array("product_name"))
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "cate_name" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "brand_name" )
            ->joinLeft ( "users_invoice", "$this->_name.business_id = users_invoice.id",array ( "business_name","contacts","mobile","qq"))
            ->joinLeft("res_currency as rc","rc.id = $this->_name.currency_id","name as currency_name")
            ->joinLeft("region","region.region_id = $this->_name.area_id","region_name as area_name")
            ->order("create_time")
            ->limit(4)
            ->__toString ();

        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchAll ( $sql );

    }
    public function getById($id){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();

        $sql = $db->select ()->from ( $this->_name )->joinLeft ( "product", "$this->_name.product_id = product.id", array("product_name"))
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "cate_name" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "brand_name" )
            ->joinLeft ( "users_invoice", "$this->_name.business_id = users_invoice.id",array ( "business_name","contacts","mobile","qq"))
            ->joinLeft("res_currency as rc","rc.id = $this->_name.currency_id","name as currency_name")
            ->joinLeft("region","region.region_id = $this->_name.area_id","region_name as area_name")
            ->where ("$this->_name.id={$id}")
            ->__toString ();
        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchRow( $sql );

    }
    /**
     * 获取单条
     * @param $id
     * @return array
     */
    public function getListById($id){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name )->joinLeft ( "product", "$this->_name.product_id = product.id", array("product_name"))
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "cate_name" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "brand_name" )
            ->joinLeft ( "users_invoice", "$this->_name.business_id = users_invoice.id",array ( "business_name","contacts","mobile","qq"))
            ->where ( $id )->__toString ();
        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchRow ( $sql );
    }
    //求购交易动态
    public function getlista(){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();

        $sql = $db->select ()->from ("iq_inquiry",array("id","status","price","DATE_FORMAT(iq_inquiry.update_time,'%h:%i') as update_time"))
            ->joinLeft ( "product", "$this->_name.product_id = product.id", array("product_name"))
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "cate_name" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "brand_name" )
            ->where("iq_inquiry.status not in(1,7)")
            ->order("iq_inquiry.update_time desc")
            ->limit("10")
            ->__toString ();

        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchAll ( $sql );
    }


    public function getCurrencyShow($where ="1=1"){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        if(!$where){
            $where=" 1=1 ";
        }
        $key   = parent::_getModuleCacheKey("getCurrencyShow_require");
        //get  Cache
        $data = $this->_cache_handler->load($key);
        if (!empty($data))
        {
            return $data;
        }

        $sql = $db->select ()->from("res_currency",array("id","name","code","mark"))->where($where)->__toString ();

        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }

        $data = $db->fetchAll( $sql );

        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;
    }

    public function getAreaShow($where ="1=1"){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        if(!$where){
            $where=" 1=1 ";
        }
        $key   = parent::_getModuleCacheKey("getAreaShow_require");
        $data = $this->_cache_handler->load($key);
        if (!empty($data))
        {
            return $data;
        }

        $sql = $db->select ()->from("iq_inquiry",array("area_id",))
            ->joinLeft("region","region.region_id = iq_inquiry.area_id","region_name as area_name")
            ->where($where)->__toString ();

        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }

        $data = $db->fetchAll( $sql );

        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;
    }

    public function getDeliveryShow($where ="1=1"){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        if(!$where){
            $where=" 1=1 ";
        }
        $key   = parent::_getModuleCacheKey("getDeliveryShow_require");
        $data = $this->_cache_handler->load($key);
        if (!empty($data))
        {
            return $data;
        }

        $sql = $db->select ()->from("iq_inquiry",array("delivery_area"))
            ->where($where)->__toString ();

        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }

        $data = $db->fetchAll( $sql );

        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;

    }

}